﻿#include "ssThreadBase.h"

namespace StarSeeker
{

ssThreadBase::ssThreadBase()
	:m_RequestStop(false)
{
}

ssThreadBase::~ssThreadBase()
{
	//RequestStop();
	WaitForExit();
}

bool ssThreadBase::StartThread()
{
	if ( m_hThread ) return false;
#ifdef SS_USE_BOOST_THREAD
	m_hThread = new boost::thread(boost::bind(&ssThreadBase::ThreadMemberFunc, this));
#elif defined _WIN32
	m_hThread = (HANDLE)_beginthreadex(0, 0, ssThreadBase::ThreadFunc, this, 0, 0);
	if (!m_hThread)
	{
		return false;
	}
#else
	if(::pthread_create( &m_hThread, 0, ssThreadBase::ThreadFunc, this ))
	{
		return false;
	}
#endif
	m_RequestStop = false;
	return true;
}

void ssThreadBase::RequestStop()
{
	m_RequestStop = true;
}

void ssThreadBase::Sleep(unsigned int ms)
{
#ifdef SS_USE_BOOST_THREAD
	boost::this_thread::sleep(boost::posix_time::milliseconds(ms));
#elif defined _WIN32
	::Sleep(ms);
#else
	::usleep(ms*1000);
#endif
}

}
